//by k1 2021.4.12 - 2021.4.16

uiSleep 22; //

if (!isNil "kk_fn_RepeatAnim") exitWith {systemChat "动作重复功能: 已有！"};

kk_RepeatAnim_pv = {
	if (!isNil "kk_fn_RepeatAnim") exitWith {};
kk_fn_RepeatAnim = {
	if (!isNil "kk_repeatPlyAnim") exitWith {};
	kk_repeatPlyAnim = 1;
	kk_repeatPlyAnim_move = _this;
	kk_repeatPlyAnim_queue = 0;
	_h = getPos player select 2;
	kk_repeatPlyAnim_env = if(_h < 5)then[{if(_h > -1)then[{1},{2}]},{3}];
	kk_repeatPlyAnim_asc = getAnimSpeedCoef player;
	kk_repeatPlyAnim_asc2 = 1;
	kk_repeatPlyAnim_state = 0;
	kk_repeatPlyAnim_legs = (player getHit "legs" < .5);
	player setVariable ["kk_repeatPlyAnim_en", true, true];
	kk_repeatPlyAnim_key = (findDisplay 46) displayAddEventHandler ["KeyDown", {if(_this select 1 == 45)then{kk_repeatPlyAnim_cancel = 1}}];
	{inGameUISetEventHandler _x} forEach [["PrevAction", "true"],["NextAction", "true"],["Action", "true"]];
	systemchat "动作重复: 开启 (X键取消)";
	["kk_repeatPlyAnim_loop", "onEachFrame", {
		if (!isNil "kk_repeatPlyAnim" && {isNil "kk_repeatPlyAnim_cancel"} && {(call {_h = getPos player select 2; if(_h < 5)then[{if(_h > -1)then[{1},{2}]},{3}]}) == kk_repeatPlyAnim_env} && {kk_repeatPlyAnim_legs isEqualTo (player getHit "legs" < .5)} && {alive player} && {player getVariable ["FAR_once_eh", true]} && {lifeState player != "INCAPACITATED"} && {lifeState player != "UNCONSCIOUS"} && {isNull objectParent player}) then [{
			kk_repeatPlyAnim_queue = kk_repeatPlyAnim_queue + 1;
			if (kk_repeatPlyAnim_queue > 10) then [{kk_repeatPlyAnim_queue = 0; player playMoveNow kk_repeatPlyAnim_move},{player playMove kk_repeatPlyAnim_move}];
			if (abs(speed player) > 0) then {
				_radGrad = [getPos player, getDir player] call BIS_fnc_terrainGradAngle;
				if (!surfaceIsWater getPosASL player && {getPos player select 2 < 5} && {_radGrad > 20 || {_radGrad < -30}}) then [{
					if ((_radGrad > 20 && {_radGrad < 33}) || {_radGrad < -30 && {_radGrad > -40}}) exitWith {if(kk_repeatPlyAnim_state != 0)then{player setAnimSpeedCoef kk_repeatPlyAnim_asc; kk_repeatPlyAnim_state = 0; kk_repeatPlyAnim_asc2 = 1}; _s = if(stance player == "PRONE")then[{2.6},{13}]; if(abs(speed player) > _s)then{kk_repeatPlyAnim_asc2 = kk_repeatPlyAnim_asc2 - .02; player setAnimSpeedCoef kk_repeatPlyAnim_asc2}};
					if (_radGrad >= 33 || {_radGrad <= -40}) exitWith {if(kk_repeatPlyAnim_state != 1)then{player setAnimSpeedCoef kk_repeatPlyAnim_asc; kk_repeatPlyAnim_state = 1; kk_repeatPlyAnim_asc2 = 1}; _s = if(stance player == "PRONE")then[{2.6},{6.5}]; if(abs(speed player) > _s)then{kk_repeatPlyAnim_asc2 = kk_repeatPlyAnim_asc2 - .02; player setAnimSpeedCoef kk_repeatPlyAnim_asc2}};
				},{
					if(getAnimSpeedCoef player != kk_repeatPlyAnim_asc)then{player setAnimSpeedCoef kk_repeatPlyAnim_asc; kk_repeatPlyAnim_asc2 = 1};
				}];
			};
			[format["<t color='#FFFFFF' size = '.5'>%1 km/h</t>", abs(round speed player)],-1,.825 * safeZoneH + safeZoneY,1,0,0,4623] spawn BIS_fnc_dynamicText;
		},{
			kk_repeatPlyAnim = nil;
			kk_repeatPlyAnim_cancel = nil;
			player switchMove kk_repeatPlyAnim_move;
			player setAnimSpeedCoef kk_repeatPlyAnim_asc;
			player setVariable ["kk_repeatPlyAnim_en", nil, true];
			(findDisplay 46) displayRemoveEventHandler ["KeyDown", kk_repeatPlyAnim_key];
			if (player getVariable ["FAR_once_eh", true]) then {{inGameUISetEventHandler _x} forEach [["PrevAction", "false"],["NextAction", "false"],["Action", "false"]]};
			["kk_repeatPlyAnim_loop", "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
			systemchat "动作重复: 关闭";
		}];
	}] call BIS_fnc_addStackedEventHandler;
};
	0 spawn {
		private ["_ply", "_repeatPlyAnim"];
		_repeatPlyAnim = -1;
		waitUntil {!isNull player};
		while {true} do {
			if (_repeatPlyAnim == -1) then {
				_ply = player;
				_repeatPlyAnim = _ply addAction [format["<t color='#00FFFF'>%1</t>", localize "STR_TAG_A_7"],{(animationState player) spawn kk_fn_RepeatAnim}, [], -1004, false, true, "", "isNil 'kk_repeatPlyAnim' && {alive player} && {isNull objectParent player} && {lifeState player != 'INCAPACITATED'} && {player getVariable ['FAR_isUnconscious', 0] == 0}"];
			};
			if (_ply != player) then {
				_ply removeAction _repeatPlyAnim;
				_repeatPlyAnim = -1;
			};
			sleep 3;
		};
	};
};
if (isServer) then [{
	publicVariable "kk_RepeatAnim_pv";
	sleep .3;
	remoteExec ["kk_RepeatAnim_pv",0,true];
	systemChat "添加动作重复功能（全局）";
},{
	call kk_RepeatAnim_pv;
	systemChat "添加动作重复功能（本地）";
}];


